package 二分查找;

public class test5 {
    public int peakIndexInMountainArray(int[] arr){
        if(arr.length==0) return -1;
        // 分成两部分,一边递增,一边递减
        int left = 0,right = arr.length-1;
        while (left<right){
            int mid = left+(right-left+1)/2;
            if(arr[mid]>=arr[mid-1]) left=mid; //mid可能是最高点
            else right=mid-1; // 所以mid-1可能是最大点
        }
        return left;
    }

    public int peakIndexInMountainArray1(int[] arr){
        if(arr.length==0) return -1;
        // 分成两部分,一边递增,一边递减
        int left = 0,right = arr.length-1;
        while (left<right){
            int mid = left+(right-left)/2;
            if(arr[mid]>=arr[mid+1]) right=mid;
            else left=mid+1;
        }
        return right;
    }
}
